package converter;

import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.convert.Converter;
import javax.faces.convert.FacesConverter;
import javax.naming.InitialContext;

import dao.DiscountCategoryDAO;

import entity.DiscountCategory;

@FacesConverter("stringToDiscountCategoryConverter")
public class StringToDiscountCategoryConverter implements Converter {

	@Override
	public Object getAsObject(FacesContext context, UIComponent component, String value) {
		DiscountCategoryDAO dao = null;
		try {
			InitialContext ctx = new InitialContext();
			dao = (DiscountCategoryDAO) ctx.lookup("java:global/PaidClinic/JPADiscountCategoryDAO!dao.DiscountCategoryDAO");
		} catch (Exception e) {
			e.printStackTrace();
		}
		return dao.selectDiscountCategoryByName(value);
	}

	@Override
	public String getAsString(FacesContext context, UIComponent component, Object value) {
		return ((DiscountCategory) value).toString();
	}

}
